namespace ScrumSprintMonitor.Infrastructure.IoC
{
    #region #using Directives

    using System;
    using System.Collections.Generic;

    using Microsoft.Practices.Unity;

    #endregion

    public interface IModule
    {
        void SetContainer(IUnityContainer container);
        void Load();
    }

    public abstract class StandardModule : IModule
    {
        private IUnityContainer container;

        protected IUnityContainer Container
        {
            get { return this.container; }
        }

        #region IModule Members

        public void SetContainer(IUnityContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (this.Container != null)
            {
                throw new InvalidOperationException();
            }

            this.container = container;
        }

        public abstract void Load();

        #endregion
    }

    public static class UnityExtensionMethods
    {
        public static void Load(this IUnityContainer container, IModule module)
        {
            module.SetContainer(container);

            module.Load();
        }

        public static void Load(this IUnityContainer container, IEnumerable<IModule> modules)
        {
            foreach (var module in modules)
            {
                module.SetContainer(container);

                module.Load();
            }
        }
    }
}